﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.圣同润.merchant;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 广州爱特安为科技.UC.圣同润.客服
{
    public partial class Form未发货订单 : DevExpress.XtraEditors.XtraForm
    {
        private String brandName;
        private int brandId;

        private List<UnDeliveryOrderLogisticsInfo> unDeliveryOrderLogisticsInfos = new List<UnDeliveryOrderLogisticsInfo>();
        public Form未发货订单()
        {
            InitializeComponent();
            this.searchLookUpEdit品牌商.Properties.DataSource = MerchantService.getUnDeliveryBrandList();
        }

        #region 选择品牌商
        private void searchLookUpEdit品牌商_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear品牌商Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit品牌商View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit品牌商.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String id = this.searchLookUpEdit品牌商.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.brandId = Convert.ToInt32(id);
                    this.brandName = this.searchLookUpEdit品牌商.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    //  下拉选品牌商，查询未发货订单
                    this.refreshData();
                }
            }
        }

        private void refreshData()
        {
            //  下拉选品牌商，查询未发货订单
            this.unDeliveryOrderLogisticsInfos = MerchantService.getUnDeliveryOrderLogisticsInfo(this.brandId);
            this.gridControl未发货订单.DataSource = this.unDeliveryOrderLogisticsInfos;
            this.gridView未发货订单.RefreshData();
            this.gridView未发货订单.BestFitColumns();
            this.gridView未发货订单.OptionsView.BestFitMode = GridBestFitMode.Fast;
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear品牌商Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit品牌商.ToolTip = null;
            searchLookUpEdit品牌商.EditValue = null;
        }
        private void searchLookUpEdit品牌商_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.brandName;
        }
        #endregion

        private void simpleButton待发货订单导出excel_Click(object sender, EventArgs e)
        {
            if (this.gridView未发货订单.DataRowCount<=0)
            {
                return;
            }
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                this.gridView未发货订单.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void 发货ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String order_id = this.gridView未发货订单.GetRowCellValue(this.gridView未发货订单.FocusedRowHandle, "order_id").ToString();
                foreach (UnDeliveryOrderLogisticsInfo item in this.unDeliveryOrderLogisticsInfos)
                {
                    if (order_id.Equals(item.order_id.ToString()))
                    {
                        new Form手动发货(item,this.brandId,this.brandName).ShowDialog();
                        break;
                    }
                }
            }
            catch { }
            this.refreshData();
        }
    }
}